<?php
/**
 * PrynLib
 * @copyright 2007-2009 Prynel
 * @author Olivier ROGER <roger.olivier@gmail.com>
 * @category libPrynel
 * @package Form
 * @version $Revision: 112 $
 */

/**
 * Element Select. 
 * @category libPrynel
 * @package Form
 * @subpackage Form_Element
 * @version 1.0.5 
 * @author Olivier ROGER <roger.olivier@gmail.com>
 * @copyright  2007-2009 Prynel
 *       
 *
 */
class Form_Element_Select extends Form_Element_Multi
{
	/**
	 * Stockage du nom en cas de select multiple
	 *
	 * @var string
	 * @access private
	 */
	private $name;
	
	/**
	 * Constructeur
	 *
	 * @param string $nom
	 * @param Form_Form $form
	 */
	public function __construct($nom,$form)
	{
		parent::__construct($nom,$form);
		$this->name = $nom;
	}
	
	/**
	 * Défini si il sagit d'un select multiple ou non
	 *
	 * @param boolean $bool
	 * @param int $size Taille du select
	 * @access public
	 * @return Form_Element_Select
	 */
	public function multiple($bool = true,$size = 5)
	{
		if($bool)
		{
			$this->attrs['multiple'] = 'multiple';
			$this->attrs['name'] = $this->attrs['name'].'[]';
			$this->attrs['size'] = $size;
		}
		else
		{
			unset($this->attrs['multiple']);
			unset($this->attrs['size']);
		}
		
		return $this;
	}
	
	/**
	 * Ecriture de l'objet
	 *
	 * @access public
	 * @return string
	 */
	public function __toString()
	{
		$css = $this->cssClass();
		$label='';	
		if(!empty($this->label))
		{
			$label = "\t".'<label for="'.$this->attrs['id'].'" class="'.$this->cssLabel.'">'.$this->label.'</label>'."\n";
			if($this->labelNewLine)
				$label.="\t".'<br />'."\n";
		}
		$attributs = $this->attrsToString();
		//Posted value ou value par défaut
		$value = $this->form->getPostedvalue($this->name);
		if($value=='')
			$value = $this->value;
		
		$options = '';
		foreach($this->choix as $itemHtml=>$itemAffichage)
		{
			if(is_array($itemAffichage)) // Cas d'un optgroup
			{
				$options.="\t\t".'<optgroup label="'.$itemHtml.'">'."\n";
				foreach($itemAffichage as $cle=>$valeur)
				{
					if(isset($this->attrs['multiple']) && is_array($value))
					{
						if(in_array($cle,$value))
							$selected = 'selected="selected"';
						else
							$selected='';
					}
					else
					{
						if($value == $cle)
							$selected = 'selected="selected"';
						else
							$selected='';
					}
					
					$options .="\t\t".'<option value="'.htmlspecialchars($cle).'" '.$selected.'>'.$valeur.'</option>'."\n";
				}
				$options.="\t\t".'</optgroup>'."\n";
				
			}
			else
			{
				if(isset($this->attrs['multiple']) && is_array($value))
				{
					if(in_array($itemHtml,$value))
						$selected = 'selected="selected"';
					else
						$selected='';
				}
				else
				{
					if($value == $itemHtml)
						$selected = 'selected="selected"';
					else
						$selected='';
				}
				
				$options .="\t\t".'<option value="'.htmlspecialchars($itemHtml).'" '.$selected.'>'.$itemAffichage.'</option>'."\n";
			}
		}
		$error = '';
		if(!is_null($this->errorMsg))
		{
			$error='<span class="'.$this->errorClass.'">'.$this->errorMsg.'</span><br />';
		}
		$field = "\t".'<select '.$attributs.' '.$css.' >'."\n".$options."\n\t".'</select>'."\n";
		if(!empty($this->info))
			$field.="\t".'<img src="'.$this->imgInfo.'" id="'.$this->attrs['name'].'_tooltip" title="'.$this->info.'" alt="" style="cursor:help;" />'."\n";
		if($this->fieldNewLine)
			$field.="\t".'<br />'."\n";
			
		return $label.$field.$error;
	}
}
 ?>